package ch.sfug.anim.tween.filter {
    import ch.sfug.anim.tween.property.PropertyTween;
    import ch.sfug.events.TweenEvent;
    import ch.sfug.geom.ColorMatrix;
    
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.filters.ColorMatrixFilter;      

    /**
     * this is a tween that animates the properties of the blurfilter.
     * 
     * @author marcel
     */
    public class HSCBTween extends Sprite {
        
        private var t               :DisplayObject;
        private var _duration       :Number;
        private var pt              :PropertyTween;
        public  var _brightness     :Number         = 0;
        public  var _contrast       :Number         = 0;
        public  var _saturation     :Number         = 0;
        public  var _hue            :Number         = 0;
               
        
        public function HSCBTween( dis:DisplayObject, d:Number ) 
        {
        	target     = dis;
        	_duration  = d;
            pt = new PropertyTween(this, d);
            pt.addEventListener(TweenEvent.TWEEN_UPDATE, updateFilters);
        }
        
               
        private function updateFilters( event:TweenEvent ):void
        {
        	var cm:ColorMatrix = new ColorMatrix();
        	cm.adjustColor(_brightness,_contrast,_saturation,_hue);
	        setObjectMatrix(target, cm)
        }
        
        private static function setObjectMatrix(p_obj:Object, p_matrix:Array): void 
        {
            var objFilters:Array = p_obj.filters.concat();
            var found:Boolean = false;
            for (var i:Number = 0; i < objFilters.length; i++) {
                if (objFilters[i] is ColorMatrixFilter) {
                    objFilters[i].matrix = p_matrix.concat();
                    found = true;
                }
            }
            if (!found) {
                var cmtx:ColorMatrixFilter = new ColorMatrixFilter(p_matrix);
                objFilters[objFilters.length] = cmtx;
            }
            p_obj.filters = objFilters;
        }
        
        
        public function hscbTo( hue:Number        = 0, 
						        contrast:Number   = 0, 
						        saturation:Number = 0, 
						        brightness:Number = 0, 
                                ease:Function = null):void 
        {
        	pt.tween( { _hue:       { from: _hue,        to: hue,        easing: ease},
        	            _contrast:  { from: _contrast,   to: contrast,   easing: ease},
        	            _saturation:{ from: _saturation, to: saturation, easing: ease},
        	            _brightness:{ from: _brightness, to: brightness, easing: ease}
        	          });
        }
        
        public function hueTo( hue:Number, ease:Function = null):void 
        {                       
            pt.tween( { _hue:{ from: _hue, to: hue,  easing: ease}});
        }
        
        public function contrastTo( contrast:Number, ease:Function = null):void 
        {                       
            pt.tween( { _contrast:{ from: _contrast, to: contrast,  easing: ease}});
        }
        
        public function saturationTo( saturation:Number, ease:Function = null):void 
        {                       
            pt.tween( { _saturation:{ from: _saturation, to: saturation,  easing: ease}});
        }
        
        public function brightnessTo( brightness:Number, ease:Function = null ):void 
        {
            pt.tween( { _brightness:{ from: _brightness, to: brightness, easing: ease}});
        }
       
               
        /**
         * returns the displayobject where the filter is applied on
         */
         
        public function stop():void 
        {
           pt.stop();
        }
        
        public function get target():DisplayObject {
            return t;
        }
        
        public function set target( trg:DisplayObject ):void {
            this.t = trg;
        }
        
        public function get duration():Number {
            return _duration;
        }
        
        public function set duration( d:Number ):void {
            _duration   = d;
            pt.duration = duration;
        }
        
                
        private static function getdefaultMatrix():Array 
        {
            return [
                1, 0, 0, 0, 0,
                0, 1, 0, 0, 0,
                0, 0, 1, 0, 0,
                0, 0, 0, 1, 0
            ];
        }
        
    }
}
